// pages/apply/apply.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: '',
    apiUrl: '',
    subjects: ['科目二-室内教学', '科目二-室外教学', '科目三'],
    type:['C1','C2','C3','B1','B2','A1','A2','A3','D/E/F'],
    objectArray: [
      {
        id: 0,
        name: '美国'
      },
      {
        id: 1,
        name: '中国'
      },
      {
        id: 2,
        name: '巴西'
      },
      {
        id: 3,
        name: '日本'
      }
    ], 
    index: 0,
    subjectIndex:0,
    typeIndex:0,
    imgUrl: 'http://static.sanfeng.maiworld.net/image/my/avatar3x.png',
    name: '',
    coachAge: null,
    idcard: null,
    phone: null,
    subject:0,
    indoor:1,
    vehicleType:1,
    status:0
  },

  submitToadmin:function(){
    // 发起网络请求
    var userinfo = wx.getStorageSync("userInfo");
    console.log(this.data.status);
    wx.request({
      url: this.data.apiUrl + '/coach/updaAuthentication.do',
      data: {
        session_key: wx.getStorageSync("session_key"),
        openid: wx.getStorageSync("openid"),
        cid: userinfo.cid,
        logo_pic: this.data.imgUrl,
        phone: this.data.phone,
        name: this.data.name,
        card_id: this.data.idcard,
        subject: this.data.subject,
        indoor: this.data.indoor,
        vehicle_type: this.data.vehicleType,
        status: this.data.status
      },
      method: 'POST',
      success: (res) => {
        console.log(res, '提交认证信息');
        wx.setStorageSync("userInfo", res.data.userInfo);
        if (res.data.userInfo.authentication == 0){
          wx.showToast({
            title: '请等待审核',
            icon:'success',
            duration:1500
          })
          wx.clearStorageSync();
          wx.setStorageSync("Auth", 3);
          setTimeout(()=>{
            wx.navigateTo({
              url: '/pages/login/login',
            })
          },1500)
        }else{
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 1500
          })
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/my/my',
            })
          }, 1500)

        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      staticUrl: app.globalData.staticUrl,
      apiUrl: app.globalData.apiUrl
    })
    var userinfo = wx.getStorageSync("userInfo");
    this.setData({
      imgUrl: userinfo.logoPic != null ? userinfo.logoPic : userinfo.headPic,
      name: userinfo.name,
      phone: userinfo.phone,
      idcard: userinfo.cardId,
    })
    if (userinfo.subject == 1){
      this.setData({
        subject: userinfo.subject,
        subjectIndex:2
      })
    } else if (userinfo.subject == 0){
      if (userinfo.indoor == 0){
        this.setData({
          subject: userinfo.subject,
          subjectIndex: 0
        })
      } else if (userinfo.indoor == 1){
        this.setData({
          subject: userinfo.subject,
          subjectIndex: 1
        })
      }
    }

    if (userinfo.vehicleType == 1){
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 0
      })
    } else if (userinfo.vehicleType == 2){
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 1
      })
    } else if (userinfo.vehicleType == 3) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 3
      })
    } else if (userinfo.vehicleType == 4) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 4
      })
    } else if (userinfo.vehicleType == 5) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 5
      })
    } else if (userinfo.vehicleType == 6) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 6
      })
    } else if (userinfo.vehicleType == 7) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 2
      })
    } else if (userinfo.vehicleType == 8) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 7
      })
    } else if (userinfo.vehicleType == 9) {
      this.setData({
        vehicleType: userinfo.vehicleType,
        typeIndex: 8
      })
    }
    console.log(this.data.vehicleType);
    this.getShool();
  },
  getShool:function(){
    // 发起网络请求
    wx.request({
      url: this.data.apiUrl + '/school/findAll.do',
      data: {
      },
      method: 'GET',
      success: (res) => {
        console.log(res, '驾校')
        this.setData({
          objectArray: res.data
        })
        var userinfo = wx.getStorageSync("userInfo");
        res.data.forEach((item,num)=>{
          if (item.id == userinfo.status){
            this.setData({
              index: num,
              status: userinfo.status
            })
          }
          if (this.data.index == num){
            this.setData({
              status: item.id
            })
          }
        })
      }
    })
  },
  uploadImg() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log(res);
        that.setData({
          imgUrl: tempFilePaths[0]
        })
        wx.uploadFile({
          url: that.data.apiUrl + '/v1/examination/upload.do', 
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res,'上传图片路径');

            var result = JSON.parse(res.data);
            that.setData({
              imgUrl: result.result
            })
          }
        })
      }
    })
  },
  inputname: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindSexChange: function (e) {
    this.setData({
      sexindex: e.detail.value
    })
  },
  inputage: function (e) {
    this.setData({
      age: e.detail.value
    })
  },
  inputcoachAge: function (e) {
    this.setData({
      coachAge: e.detail.value
    })
  },
  inputidcard: function (e) {
    this.setData({
      idcard: e.detail.value
    })
  },
  inputvehicleType: function (e) {
    this.setData({
      vehicleType: e.detail.value
    })
  },
  inputlicensePlate: function (e) {
    this.setData({
      licensePlate: e.detail.value
    })
  },
  inputphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var item = this.data.objectArray[e.detail.value];
    this.setData({
      index: e.detail.value,
      status: item.id
    })
  },
  bindsubjectsChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      subjectIndex: e.detail.value
    })
    if (e.detail.value ==0){
      this.setData({
        //科目二  室内
        subject: 0,
        indoor: 0,
      })
    } else if (e.detail.value == 1){
        //科目二  室外
      this.setData({
        subject: 0,
        indoor: 1,
      })
    } else if (e.detail.value == 2) {
      //科目三
      this.setData({
        subject: 1,
        indoor: 1,
      })
    }
  },
  bindtypeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      typeIndex: e.detail.value
    })

    if (e.detail.value == 0){
      this.setData({
        vehicleType:1
      })
    } else if (e.detail.value == 1){
      this.setData({
        vehicleType: 2
      })
    } else if (e.detail.value == 2) {
      this.setData({
        vehicleType: 7
      })
    } else if (e.detail.value == 3) {
      this.setData({
        vehicleType: 3
      })
    } else if (e.detail.value == 4) {
      this.setData({
        vehicleType: 4
      })
    } else if (e.detail.value == 5) {
      this.setData({
        vehicleType: 5
      })
    } else if (e.detail.value == 6) {
      this.setData({
        vehicleType: 6
      })
    } else if (e.detail.value == 7) {
      this.setData({
        vehicleType: 8
      })
    } else if (e.detail.value == 8) {
      this.setData({
        vehicleType: 9
      })
    }

    console.log(this.data.vehicleType,'车型')
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // var userinfo = wx.getStorageSync("userInfo");
    // if (userinfo.authentication == 0){

    // }else{
    //   wx.switchTab({
    //     url: '/pages/index/index',
    //   })
    // }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})